import Mock from 'mockjs'


const userList = {  //定义用户数据
    data: {
        total: 6,
        //前两个用户数据分别固定设为管理员和普通用户，为后续权限设置做准备，其他用户随机生成
        userinfo: [{
            username: 'admin',
            password: '123456',
            token: '000111222333444555666',
            id: '100',
        }, {
            username: '123',
            password: '123456',
            token: '145145145123123123111',
            id: '101',
        },
        {
            username: '@word(3, 5)',
            password: '123456',
            token: '@guid()',
            id: '102',
        },],
        meta: {
            status: 200,
            message: 'success',
        }
    },
};
// 登录接口
Mock.mock('/user_login', 'post', req => { //路径与请求方式
    const { username, password } = JSON.parse(req.body); //将传递进来的数据保存
    for (let i = 0; i < userList.data.userinfo.length; i++) {
        //判断userList中是否存在该用户并且用户密码是否正确
        if (username == userList.data.userinfo[i].username && password == userList.data.userinfo[i].password) {
            return {
                meta: {
                    msg: '登录成功！！！',
                    status: 200,
                    token: userList.data.userinfo[i].token
                },
                user: {
                    // 登陆成功返回信息
                    username: userList.data.userinfo[i].username,
                }
            }
        }
    }
    return {
        meta: {
            msg: '登录失败！！！',
            status: 201
        }
    }
})

// 注册接口
Mock.mock('/user_register', 'post', req => {
    // 获取传过来的数据
    const { username, password } = JSON.parse(req.body);
    // 判断是否需要注册
    // console.log(req.body);
    for (let i = 0; i < userList.data.userinfo.length; i++) {
        if (userList.data.userinfo[i].username === username) {
            return {
                meta: {
                    msg: '该用户已经存在，请重新输入用户名',
                    status: 201
                }
            }
        } else {
            return {
                req: req,
                meta: {
                    msg: '注册成功！！！',
                    status: 200
                },
                user: {
                    // 注册成功返回信息
                    username: username
                }
            }
        }

    }
})

// 获取所有用户图片信息
// 定义数据
const image_list = Mock.mock({
    code: 200,
    "userinfo|4": [{
        // Mock.mock('')
        name: '@cname',
        time: '@date("yyyy-MM-dd")',
        haad_pic: 'https://img1.baidu.com/it/u=592570905,1313515675&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1667494800&t=c3d8aa418bb59bbb86dafbf5c9b1768b',
        image_list: [
            "https://img1.baidu.com/it/u=1014700809,3181937698&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500",
            "https://img0.baidu.com/it/u=1476024793,2116268051&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=382",
            "https://img2.baidu.com/it/u=1168536641,200612244&fm=253&fmt=auto&app=138&f=JPEG?w=583&h=300",
            "https://img2.baidu.com/it/u=3007571391,1465142310&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=313",
            "https://img0.baidu.com/it/u=1476024793,2116268051&fm=253&fmt=auto&app=120&f=JPEG?w=1024&h=382",
            "https://img1.baidu.com/it/u=1014700809,3181937698&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=500",
        ]
    }]
})
Mock.mock('/api/image_list', "get", req => {
    return image_list;
})


// 投票管理数据模拟
const vote = Mock.mock({
    code: 200,
    'votelist|10': [{
        "id|+1": 1,//序号
        "title|1": ['中秋礼物选择', '每天读书半小时投票活动', '每天运动半小时投票活动'],//标题
        "state|1": ['进行中', '已结束'],
        time: '@date(yyyy-MM-dd )',
        operation: '删除'
    }]
})

Mock.mock('/api/vote', 'get', () => {
    return vote;
})
